﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using PlasmaMes.A_Models;
using PlasmaMes.A_Models.ResponseClass;

namespace PlasmaMes.Services
{
    public class MesService
    {
        private static MesService instance;

        private static readonly object locker = new object();

        private MesService() { }


        public static MesService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        instance = new MesService();
                    }

                }
                return instance;
            }
        }

        public string responseBody = string.Empty;




        public static async Task GetToken()
        {
            TokenGet tokenGet = new TokenGet();
            tokenGet.userCode = RunParam.Instance.UserCode;
            tokenGet.factoryCode = RunParam.Instance.FactoryCode;
            tokenGet.timeStamp = DateTime.Now.ToString();


            await PostRequest(RunParam.Instance.Url, tokenGet);

            TokenResponse tokenResponse = JsonHelper.JsonStringToObj<TokenResponse>(instance.responseBody);

            RunParam.Instance.Token = tokenResponse.Data.Token;
        }


        public static async Task GetSn()
        {
            ProductSnGet productSnGet = new ProductSnGet();
            productSnGet.StationCode = RunParam.Instance.StationCode;
            productSnGet.StageCode = RunParam.Instance.StageCode;
            productSnGet.ToolCode = RunParam.Instance.ToolCode;

            await PostRequest(RunParam.Instance.Url, productSnGet);

            SNResponse sNResponse = JsonHelper.JsonStringToObj<SNResponse>(instance.responseBody);

            if(sNResponse.Code == 200)//成功
            {
                if(sNResponse.DataList.Count == 1)
                {
                    RunParam.Instance.SnCount = 1;
                    RunParam.Instance.Sn1 = sNResponse.DataList[0].Sn;
                }
                else if(sNResponse.DataList.Count == 2)
                {
                    RunParam.Instance.SnCount = 2;
                    RunParam.Instance.Sn1 = sNResponse.DataList[0].Sn;
                    RunParam.Instance.Sn2 = sNResponse.DataList[1].Sn;
                }
            }
            else
            {

            }

        }


        public static async Task SnPassStation()
        {
            for(int i = 0; i < RunParam.Instance.SnCount; i++)
            {
                BarcodePass barcodePass = new BarcodePass();
                barcodePass.token = RunParam.Instance.Token;
                barcodePass.AssetNo = RunParam.Instance.AssetNo;
                barcodePass.StageCode = RunParam.Instance.StageCode;
                barcodePass.StationCode = RunParam.Instance.StationCode;
                if(i == 0)
                 barcodePass.Sn = RunParam.Instance.Sn1;

                if (i == 1)
                    barcodePass.Sn = RunParam.Instance.Sn2;

                barcodePass.IsFinished = "0";
                barcodePass.DeductMaterials = "0";


                await PostRequest(RunParam.Instance.Url, barcodePass);

                PassResponse passResponse = JsonHelper.JsonStringToObj<PassResponse>(instance.responseBody);
                if(passResponse.Code == 200)
                {
                    LogService.Instance.LogInfo($"{passResponse.Message}");
                }
                else if(passResponse.Code == 500)
                {
                    LogService.Instance.LogWarning($"{passResponse.Message}");
                }

               
            }
            
        }


        public static async Task GetRequest(string url, object obj)
        {
            using (HttpClient client = new HttpClient())
            {
                // 替换为你的目标URL
                // string url = "http://example.com/api/resource";

                try
                {
                    HttpResponseMessage response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\n异常捕获!");
                    Console.WriteLine(e.Message);
                }
            }
        }


        public static async Task PostRequest(string url, object obj)
        {
            using (HttpClient client = new HttpClient())
            {

                var content = new StringContent(JsonHelper.ObjToJsonString(obj), Encoding.UTF8, "application/json");

                try
                {
                    HttpResponseMessage response = await client.PostAsync(url, content);
                    response.EnsureSuccessStatusCode();
                    string rp = await response.Content.ReadAsStringAsync();

                    instance.responseBody = rp;

                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\n异常捕获!");
                    Console.WriteLine(e.Message);
                }
            }
        }

    }
}
